package oop.ex4.crosswords;

import java.util.Comparator;

/**
 * A comparator that compares entries by their length then by their x position,
 * then by their y position and last by vertical or horizontal in that order. 
 * @author yomach
 * @author mbattat
 */
public class EntriesComparator implements Comparator<CrosswordEntry> {
	
	@Override
	public int compare(CrosswordEntry entry1, CrosswordEntry entry2) {
		return compareEntriesLocation(entry1, entry2);
	}
	
	/**
	 * Compares entries by their length then by their x position, then 
	 * by their y position and last by vertical or horizontal in that order. 
	 * @param entry1 The first entry.
	 * @param entry2 The second entry.
	 * @return A negative integer, zero, or a positive integer as the 
	 * first argument is less than, equal to, or greater than the second. 
	 */
	public static int compareEntriesLocation(CrosswordEntry entry1, CrosswordEntry entry2) {
		CrosswordPosition pos1 = entry1.getPosition();
		CrosswordPosition pos2 = entry2.getPosition();
		if (entry1.getLength() > entry2.getLength()) { return 1; }
		if (entry1.getLength() < entry2.getLength()) { return -1; }
		if (pos1.getX() > pos2.getX()) { return 1;	}
		if (pos1.getX() < pos2.getX()) { return -1;	}
		if (pos1.getY() > pos2.getY()) { return 1; }
		if (pos1.getY() < pos2.getY()) { return -1; }
		if (pos1.isVertical() && !pos2.isVertical()) { return 1; }
		if (!pos1.isVertical() && pos2.isVertical()) { return -1; }	
		else { return 0; }
	}
}
